package com.mjw.tender.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mjw.tender.api.BlogUserService;
import com.mjw.tender.domain.BlogUser;
import com.mjw.tender.domain.TbUser;
import com.mjw.tender.dto.ResponseResult;
import com.mjw.tender.dto.TbUserDto;
import com.mjw.tender.feign.Oauth2Feign;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Reference(version = "1.0.0")
    BlogUserService blogUserService;

    @Resource
    Oauth2Feign oauth2Feign;

    @Resource
    PasswordEncoder passwordEncoder;

    /**
     * 通过token获取用户信息
     * token 以param的方式传输
     * @return
     */
    @GetMapping("/")
    public ResponseResult getUserInfo(){
        ResponseResult<HashMap> userInfo = oauth2Feign.getUserInfo();
        if(userInfo.getCode()!= ResponseResult.CodeStatus.OK){
            return userInfo;
        }
        HashMap map =userInfo.getData();
        ObjectMapper mapper = new ObjectMapper();
        TbUser tbUser = mapper.convertValue(map.get("user"),TbUser.class);

        if(null == tbUser){
            return new ResponseResult(ResponseResult.CodeStatus.ILLEGAL_TOKEN,"查询用户失败，请重新登录");
        }
        BlogUser blogUser = blogUserService.get(tbUser.getId());
        return new ResponseResult(ResponseResult.CodeStatus.OK,blogUser);
    }


    @PutMapping("/")
    public ResponseResult modifyUser(@RequestBody BlogUser blogUser){
        int i = blogUserService.modify(blogUser);
        return new ResponseResult<>(ResponseResult.CodeStatus.OK,"修改成功");
    }

    /**
     * TODO 分布式事务
     * @param tbUser
     * @return
     */
    @PostMapping("/")
    public ResponseResult addUser(@RequestBody TbUser tbUser){
        tbUser.setPassword(passwordEncoder.encode(tbUser.getPassword()));
        tbUser.setCreated(new Date());
        tbUser.setType(1);//设置为博客门户账号
        ResponseResult responseResult = oauth2Feign.addUserReturnId(tbUser);
        if(responseResult.getCode()== ResponseResult.CodeStatus.OK){
            Long id = (Long) responseResult.getData();
            tbUser.setId(id);
            BlogUser blogUser = change(tbUser);
            int i = blogUserService.add(blogUser);
            return new ResponseResult(ResponseResult.CodeStatus.OK,"注册成功");
        }else {
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,"注册失败");
        }
    }

    @GetMapping(value = "/",params = {"nickname"})
    public ResponseResult getByNickname(@RequestParam("nickname")String nickname){
        BlogUser blogUser = blogUserService.getByNickname(nickname);
        return new ResponseResult(ResponseResult.CodeStatus.OK,blogUser);
    }

    @GetMapping(value = "/",params = {"username"})
    public ResponseResult getByUserName(@RequestParam("username")String username){
        BlogUser blogUser = blogUserService.getByUserName(username);
        return new ResponseResult(ResponseResult.CodeStatus.OK,blogUser);
    }

    @GetMapping(value = "/",params = "shareFlag")
    public ResponseResult<List<BlogUser>> getByShareFlag(@RequestParam("shareFlag")boolean shareFlag){
        List<BlogUser> list = blogUserService.getByShareFlag(shareFlag);
        return new ResponseResult<>(ResponseResult.CodeStatus.OK,list);
    }

    private BlogUser change(TbUser tbUser){
        BlogUser blogUser = new BlogUser();
        blogUser.setUsername(tbUser.getUsername());
        blogUser.setNickname(tbUser.getUsername());
        blogUser.setTbUserId(tbUser.getId());
        blogUser.setEmail(tbUser.getEmail());
        return blogUser;
    }
}
